package com.hp.bon.sgw.start;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.bon.sgw.Constants;
import com.hp.bon.sgw.service.LoadBusinessRecordService;
import com.hp.bon.sgw.util.DeamonApplicationHelper;
import com.hp.bon.sgw.util.LogTool;

/**
 * 负责将业务数据记录从*.sgwdata文件中发布/写入到Oracle库中。
 * 
 * @author wuzhih
 * 
 */
public class DBRecPublisher {

	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(DBRecPublisher.class);
	private DeamonApplicationHelper _daemon = new DeamonApplicationHelper(DBRecPublisher.class.getName());

	private char _status = Constants.SystemStatus.MAINTENANCE;

	public static void main(String args[]) throws Exception {
		DBRecPublisher dbBRecPublisher = _INSTANCE;
		dbBRecPublisher.start(args);
	}

	public static DBRecPublisher getInstance() {
		return _INSTANCE;
	}

	static private DBRecPublisher _INSTANCE = new DBRecPublisher();

	private DBRecPublisher() {
	}

	public char getStatus() {
		return _status;
	}

	public void start(String[] args) {
		LogTool.installSLF4JBridge();

		logger.info("*************** start db publisher process ***************");

		String propFile = null;
		if (args.length > 0) {
			propFile = args[0];
		} else {
			propFile = "etc/sgw.properties";
		}
		LogTool.loadProperty(propFile);

		LoadBusinessRecordService.getInstance().start();
		// LoadJsonDataService.getInstance().start();

		_daemon.waitExit();

		_status = Constants.SystemStatus.MAINTENANCE;
		logger.info(">>>>> stopping db publisher ...");
		LoadBusinessRecordService.getInstance().exit();
		// LoadJsonDataService.getInstance().exit();
		logger.info("*************** db publisher Stopped ***************");
		_status = Constants.SystemStatus.OFFLINE;
		System.exit(0);
	}

}
